package cn.ecut.controller;
import java.util.*;


import cn.ecut.model.*;
import cn.ecut.service.ShoufeibiaozhunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/shoufeibiaozhun")
public class ShoufeibiazhunController {

    @Autowired
    private ShoufeibiaozhunService shoufeibiaozhunService;

    @ResponseBody
    @RequestMapping("/selectAllshoufeibiaozhun")
    public HashMap<String, Object> selectAllshoufeibiaozhun(){
        HashMap<String, Object> map = new HashMap<String, Object>();
        Integer count = shoufeibiaozhunService.selectAllcount();
        List<Shoufeibiaozhun> list = new ArrayList < Shoufeibiaozhun>();
        list=shoufeibiaozhunService.selectAll();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        return map;
    }

    @ResponseBody
    @RequestMapping("/add")
    public Message add(Shoufeibiaozhun shoufeibiaozhun){
        Message message = new Message();
        shoufeibiaozhun.setShoufeibiaozhunma(shoufeibiaozhunService.getshoufeibiaozhunMa());
        int n = shoufeibiaozhunService.insert(shoufeibiaozhun);
        if (n == 1) {
            message.setFlag(true);
            return message;
        } else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/update")
    public Message update(Shoufeibiaozhun shoufeibiaozhun){
        Message message = new Message();
        int n = shoufeibiaozhunService.updateByPrimaryKey(shoufeibiaozhun);
        if (n == 1) {
            message.setFlag(true);
            return message;
        } else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/select")
    public HashMap select(String searchResult){
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Shoufeibiaozhun> list = new ArrayList<>();
        list = shoufeibiaozhunService.advanceSelect(searchResult);
        Integer count = list.size();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Message delete(String shoufeibiaozhunma){
        Message message = new Message();
        int n = shoufeibiaozhunService.deleteByPrimaryKey(shoufeibiaozhunma);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/qiyong")
    public Message qiyong(Shoufeibiaozhun shoufeibiaozhun){
        Message message = new Message();
        if(shoufeibiaozhun.getQiyongzhuangtai()==0){
            shoufeibiaozhun.setQiyongzhuangtai(1);
        }else {
            shoufeibiaozhun.setQiyongzhuangtai(0);
        }
        int n = shoufeibiaozhunService.updateQiyong(shoufeibiaozhun);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }
}
